package com.jinzhi.jzweb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jinzhi.jzweb.domain.CertificateInfoDO;
import com.jinzhi.jzweb.domain.cond.WorkExperinceCond;
import com.jinzhi.jzweb.domain.vo.CertificateInfoVo;
import com.jinzhi.jzweb.domain.vo.WorkExperinceVo;
import com.jinzhi.sys.domain.UserDO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jinzhi.jzweb.dao.WorkExperinceDao;
import com.jinzhi.jzweb.domain.WorkExperinceDO;
import com.jinzhi.jzweb.service.WorkExperinceService;
import com.jinzhi.common.base.CoreServiceImpl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *
 * </pre>
 * <small> 2024-05-15 17:22:11 | lance</small>
 */
@Service
public class WorkExperinceServiceImpl extends CoreServiceImpl<WorkExperinceDao, WorkExperinceDO> implements WorkExperinceService {

    @Autowired
    private WorkExperinceDao workExperinceDao;

    @Override
    public void saveUserExperince(UserDO user, WorkExperinceCond cond) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        LocalDate end = LocalDate.parse(cond.getEndTime(), formatter);
        LocalDate start = LocalDate.parse(cond.getStartTime(), formatter);
        WorkExperinceDO workExperinceDO = new WorkExperinceDO();
        BeanUtils.copyProperties(cond, workExperinceDO);
        workExperinceDO.setEndTime(end);
        workExperinceDO.setStartTime(start);
        workExperinceDO.setInfoUserId(user.getId());
        workExperinceDao.insert(workExperinceDO);
    }

    @Override
    public void updateUserExperince(WorkExperinceCond cond) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        WorkExperinceDO workExperinceDO = this.selectById(cond.getId());
        workExperinceDO.setWorkUnit(cond.getWorkUnit());
        workExperinceDO.setContactMobile(cond.getContactMobile());
        workExperinceDO.setJob(cond.getJob());
        workExperinceDO.setContact(cond.getContact());
        LocalDate end = LocalDate.parse(cond.getEndTime(), formatter);
        LocalDate start = LocalDate.parse(cond.getStartTime(), formatter);
        workExperinceDO.setEndTime(end);
        workExperinceDO.setStartTime(start);
        workExperinceDao.updateById(workExperinceDO);
    }

    @Override
    public List<WorkExperinceVo> getUserExperince(UserDO user) {
        List<WorkExperinceDO> infoUserId = this.selectList(new EntityWrapper<WorkExperinceDO>().eq("info_user_id", user.getId()));
        List<WorkExperinceVo> voList = new ArrayList<>();

        infoUserId.forEach(e -> {
            WorkExperinceVo vo = new WorkExperinceVo();
            BeanUtils.copyProperties(e, vo);
            vo.setEndTime(e.getEndTime().toString().replace("-","/"));
            vo.setStartTime(e.getStartTime().toString().replace("-","/"));
            voList.add(vo);
        });
        return voList;
    }
}
